<?php

class Painel_PermissaoController extends App_Controller_Action
{
    
    private $_table     = 'PERMISSAO';
    private $_pk        = 'PER_ID';
    private $_order_col = 'PER_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'PER_STA';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Painel_Form_Permissao();
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        
        $where = 'WHERE '.$this->_pk . ' > 0';
        
        $dscfiltro = $this->_getParam('dscfiltro', '');
        if($dscfiltro > '') $where .= ' and PER_NOM like "%'. $dscfiltro.'%" ';
        $this->view->dscfiltro = $dscfiltro;
        
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
         
    
    public function editAction() {
       
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_pdr.js');
        
        
        Zend_Loader::loadClass('dominio','library');
        
        $id = $this->_getParam('id', 0);
        
        $this->form = new Painel_Form_Permissao(null, $id);
        $this->view->form = $this->form;
        
        $this->view->PER_ID = $id;
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $arr_ck = $formData['MOD_ID'];
            try {
                if ($this->form->isValid($formData)) {

                    $id = (int) $this->form->getValue('PER_ID');

                    $data = array(
                        'PER_ID'    => $this->form->getValue('PER_ID'),
                        'PER_NOM'   => $this->form->getValue('PER_NOM'),
                        'PER_STA'   => $this->form->getValue('PER_STA'),
                    );

                    $this->db->beginTransaction();
                    if ($id == 0){
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();  
                    }
                    else{
                        $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);
                    }

                    $this->db->delete('PERMISSAO_HAS_MODULO', $this->_pk . ' = ' . $id);
                    foreach($arr_ck as $ck){
                        $data_permissao = array(
                            'PER_ID' => $id,
                            'MOD_ID' => $ck,
                            'MOD_ADD' => isset($formData['MOD_ADD'][$ck]) ? $formData['MOD_ADD'][$ck]:0,
                            'MOD_EDT' => isset($formData['MOD_EDT'][$ck]) ? $formData['MOD_EDT'][$ck]:0,
                            'MOD_DEL' => isset($formData['MOD_DEL'][$ck]) ? $formData['MOD_DEL'][$ck]:0,
                        );
                        $this->db->insert('PERMISSAO_HAS_MODULO', $data_permissao);
                    }

                    $this->db->commit();

                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                //Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
                $this->db->rollBack();
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk .' = ' . $id;
                $result = $this->db->fetchRow($query);
                
                $this->form->populate($result);
                $this->view->obj = $result;
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $msg = '';
        $id = $this->_getParam('id', 0);
        if((int) $id > 0 ) {   
            $this->db->beginTransaction();
            try {   
            
                $ok = (bool) $this->db->delete('PERMISSAO_HAS_MODULO', $this->_pk . ' = ' . $id);

                $ok = (bool) $this->db->delete($this->_table, $this->_pk . ' = ' . $id);
            
                $this->db->commit();
            } catch(Exception $ex) {
                //echo getMessage();
                $ok = FALSE;
                $msg = MSG_DELETE;
                $this->db->rollBack();
            }          
        }     
        echo json_encode(array('ok' => $ok, 'msg'=> $msg));
    }

   



}